package com.gxlevi.controller;

import com.alibaba.fastjson.JSON;
import com.gxlevi.bean.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
public class WebDataController {
    @Autowired
    KafkaTemplate kafkaTemplate;

    @RequestMapping("/receive")
    public Map<String, String> receive(@RequestBody String json) {
        Map<String, String> map = new HashMap<>();
        try {
            Message msg = new Message();
            msg.setCount(1);
            msg.setTimeStamp(System.currentTimeMillis());
            msg.setMessage(json);

            String msgJSON = JSON.toJSONString(msg);
            kafkaTemplate.send("gxlevi", msgJSON);
            map.put("success", "true");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("success", "false");
        }
        return map;
    }
}
